package com.bandaotixi.cashier.api.controller;

import cn.hutool.core.io.FileUtil;
import com.bandaotixi.cashier.api.business.FileService;
import com.bandaotixi.cashier.api.business.bo.FileInfo;
import com.bandaotixi.cashier.api.common.result.Result;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件控制层
 */
@RestController
@RequestMapping("/files")
@RequiredArgsConstructor
public class FileController {

    private final FileService fileService;

    @PostMapping
    public Result<FileInfo> uploadFile(@RequestPart(value = "file") MultipartFile file) throws IOException {
        FileInfo fileInfo = fileService.uploadMD5(file.getInputStream(), FileUtil.getSuffix(file.getOriginalFilename()));
        return Result.success(fileInfo);
    }

    @DeleteMapping
    @SneakyThrows
    public Result<?> deleteFile(@RequestParam String filePath) {
        boolean result = fileService.deleteFile(filePath);
        return Result.judge(result);
    }
}
